<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
    }
}
?>
<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>QRC - Building Management</title>
        <link rel="icon" type="image/ico" href="../images/favicon.ico" />
        <!-- Bootstrap Core CSS -->
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/jquery.toastmessage.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="../css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="../css/sb-admin-2.css" rel="stylesheet">
        <!-- Custom Fonts -->
        <link href="../font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="../css/minoral.css">
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
        <style type="text/css">
            .spinner {
                width: 70px;
                text-align: center;
            }
            #spinnerCE {
                width: 70px;
                text-align: center;
            }
            #spinnerCE > div {
                width: 18px;
                height: 18px;
                background-color: #333;

                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }
            .spinner > div {
                width: 18px;
                height: 18px;
                background-color: #333;

                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }
            #spinnerCE .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            #spinnerCE .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }
            .spinner .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            .spinner .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }
            @-webkit-keyframes bouncedelay {
                0%, 80%, 100% { -webkit-transform: scale(0.0) }
                40% { -webkit-transform: scale(1.0) }
            }

            @keyframes bouncedelay {
                0%, 80%, 100% { 
                    transform: scale(0.0);
                    -webkit-transform: scale(0.0);
                } 40% { 
                    transform: scale(1.0);
                    -webkit-transform: scale(1.0);
                }
            }

        </style>
    </head>

    <body>

        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="../project-page/index-dashboard.php"><img src="../images/qrc_logo.JPG" height="30"/> QRC-Building Management</a>
                </div>
                <!-- /.navbar-header -->

                <ul class="nav navbar-top-links navbar-right">
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i> <?= $_SESSION['username']; ?> <i class="fa fa-caret-down"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
                            </li>
                            <li><a href="../setting-page/index-setting.php"><i class="fa fa-gear fa-fw"></i> Settings</a>
                            </li>
                            <li class="divider"></li>
                            <li><a href="#" id="logout_click"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
                            </li>
                        </ul>
                        <!-- /.dropdown-user -->
                    </li>
                    <!-- /.dropdown -->
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <div id="left-menu-load"></div>
                    </div>
                    <!-- /.sidebar-collapse -->
                </div>
                <!-- /.navbar-static-side -->
            </nav>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">Work Order (มอบหมาย)</h1>
                        <ol class="breadcrumb">
                            <li>
                                <a href="../assign-page/index-assign.php"><i class="fa fa-tasks"></i> Work Order (มอบหมาย)</a>
                            </li>
                            <li class="active">
                                <i class="fa fa-tasks"></i> Create Work Order
                            </li>
                        </ol>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <div class="row" id="load_all_status">

                </div>
                <div class="row" id="team_tbl_content">

                    <div class="col-lg-8">
                        <div class="panel panel-primary">
                            <div class="panel-heading">
                                <h3 class="panel-title"><i class="fa fa-clock-o fa-fw"></i> Create WO Assignment</h3>
                            </div>
                            <div class="panel-body">
                                <div class="alert alert-danger" id="alert_inform">
                                    <strong>Please enter/select require fields</strong> 
                                    <span id="alert_information">Change a few things up and try submitting again.</span>
                                </div>
                                <div class="table-responsive">
                                    <div class="col-md-12">
                                        <label for="project_plan_size">WO Name (ชื่องาน)</label>
                                        <input type="text" id="wo_name" name="wo_name" class="col-md-6 form-control" disabled="true"/>
                                        <div class="separator"></div>
                                    </div>
                                    <div class="col-md-12">
                                        <label for="project_code">Project Code (หมายเลขโครงการ)</label>
                                        <!-- <input type="text" id="project_code_orderPage" name="project_code" class="col-md-6 form-control" disabled="true" /> -->
                                        <select class="form-control" id="project_code_orderPage" name="project_code_orderPage">
                                            <option value="0"></option>
                                            <?php
                                            $sqlSelectProjectType = "SELECT * FROM QRC_PROJECT;";
                                            $resultSet = mysql_query($sqlSelectProjectType);
                                            while ($row = mysql_fetch_array($resultSet)) {
                                                echo '<option value="' . $row['project_code'] . '">' . $row['project_name'] . '</option>';
                                            }
                                            ?>      
                                        </select>
                                        <div class="separator"></div>
                                    </div>
                                    <div class="col-md-12">
                                        <label for="project_order_status">Status (สถานะ)</label>
                                        <select class="form-control" id="project_order_status" name="project_order_status" disabled="true">
                                            <option value="New">New</option>
                                            <option value="Assign">Assign</option>
                                            <option value="Pending">Pending</option>
                                            <option value="Cancel">Cancel</option>
                                            <option value="Complete">Complete</option>
                                            <option value="Close">Close</option>
                                        </select>
                                        <div class="separator"></div>
                                    </div>
                                    <div class="separator"></div>
                                    <div id="assign_case">

                                        <form id="assign_detail_in_order_page">
                                            <div class="col-md-12">
                                                <label for="project_plan_size">Project Manager (ผู้จัดการโครงการ)</label>
                                                <input type="text" id="wo_project_managers" name="project_plan_size" class="col-md-6 form-control" disabled="true"/>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_unit_price">Project Foreman (ผู้ควบคุมงานของโครงการ)</label>
                                                <input type="text" id="wo_project_foreman" name="wo_project_foreman" class="col-md-6 form-control" disabled="true"/>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_amount">Supervisor Control (ผู้ควบคุมงานของลูกค้า)</label>
                                                <input type="text" id="wo_supervisor_control" name="wo_supervisor_control" class="col-md-6 form-control" disabled="true"/>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12" id="team_code_error">
                                                <label for="project_vat">Team Code (หมายเลขทีมช่าง) *</label>
                                                <!--<input type="text" id="wo_team_code" name="wo_team_code" class="col-md-6 form-control"/>-->
                                                <select class="form-control" id="wo_team_code" name="wo_team_code">
                                                    <option value="0"></option>
                                                    <?php
                                                    $sqlSelectProjectType = "SELECT * FROM QRC_TEAM_BUILDER;";
                                                    $resultSet = mysql_query($sqlSelectProjectType);
                                                    while ($row = mysql_fetch_array($resultSet)) {
                                                        echo '<option value="' . $row['tCode'] . '">' . $row['tCode'] . '</option>';
                                                    }
                                                    ?>      
                                                </select>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_vat">Team Name (ชื่อทีม) *</label>
                                                <input type="text" id="wo_team_name" name="wo_team_name" class="col-md-6 form-control" disabled="true"/>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_vat">Assign Date (วันที่)</label>
                                                <input type="text" id="wo_assign_date" name="wo_assign_date" class="col-md-6 form-control" disabled="true"/>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_vat">Target Date (วันที่)</label>
                                                <input type="text" id="wo_target_date" name="wo_target_date" class="col-md-6 form-control search_date" data-date-format="yyyy-mm-dd"/>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_vat">Remark</label>
                                                <input type="text" id="wo_remark" name="wo_remark" class="col-md-6 form-control"/>
                                                <div class="separator"></div>
                                            </div>
                                        </form>
                                    </div>
                                    <div id="complete_case">
                                        <form id="complete_detail_in_order_page">
                                            <div class="col-md-12">
                                                <label for="inspection_order_type_form">Inspection No. (เลขที่ใบสั่งจ้าง):</label>
                                                <select class="form-control" id="inspection_order_type_form" name="inspection_order_type_form">
                                                    <option value=""></option>
                                                    <?php
                                                    $sqlSelectMemType = "SELECT * FROM QRC_INSPECTION;";
                                                    $resultSets = mysql_query($sqlSelectMemType);
                                                    while ($row = mysql_fetch_array($resultSets)) {
                                                        echo '<option value="' . $row['INS_ID'] . '">' . $row['INS_INSPECTION_NO'] . '</option>';
                                                    }
                                                    ?>
                                                </select>
                                                <div class="separator"></div>
                                            </div>
                                            <div class="col-md-12">
                                                <label for="project_unit_price">Inspection Date (วันที่):</label>
                                                <input type="text" class="form-control" id="inspection_date_form" name="inspection_date_form" data-date-format="yyyy-mm-dd" placeholder="yyyy-mm-dd" disabled="true">
                                                <div class="separator"></div>
                                            </div>
                                        </form>
                                    </div>
                                    <div class="col-md-12">
                                        <label for="wo_order_type">WO. Order Type</label>
                                        <select class="form-control" id="wo_order_type" name="wo_order_type">
                                            <option value="ติดตั้ง">ติดตั้ง</option>
                                            <option value="แก้ไข">แก้ไข</option>
                                            <option value="ซ่อมแซม">ซ่อมแซม</option>
                                            <option value="รื้อถอน">รื้อถอน</option>
                                        </select>
                                        <div class="separator"></div>
                                    </div>
                                    <div class="col-md-12">
                                        <label for="project_po_no">PO Code. (เลขที่ใบสั่งจ้าง)</label>

                                        <select class="form-control" id="po_no_no" name="po_no_no">

                                        </select>
                                        <div class="separator"></div>
                                    </div>
                                    <div class="col-md-12">
                                        <label for="project_document_no">WO Price</label>
                                        <div class="form-group input-group">
                                            <span class="input-group-addon">฿</span>
                                            <input type="text" id="wo_price" class="form-control">
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <label for="project_plan">% Of PO</label>
                                        <div class="form-group input-group">
                                            <span class="input-group-addon">%</span>
                                            <input type="text" id="perc_of_po" class="form-control">
                                        </div>
                                    </div>
                                    <div class="col-md-12" id="shown_remark">
                                        <label for="project_order_remark">Remark</label>
                                        <textarea rows="4" cols="30" class="col-md-6 form-control" id="project_order_remark" name="project_order_remark"></textarea>
                                        <div class="separator"></div>
                                    </div>
                                    <div class="col-md-12 " id="shown_remark">
                                        <button class = "btn btn-red pull-right" style="margin-top: 10px;" id="reset_wo_form">Cancel (ยกเลิก)</button> 

                                        <button class = "btn btn-primary pull-right" style="margin-top: 10px; margin-right: 10px;" id="save_wo_form">Save (บันทึก)</button>                                                                                

                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="panel panel-primary">
                            <div class="panel-heading">
                                <h3 class="panel-title"><i class="fa fa-angle-double-right"></i> PO Detail</h3>
                            </div>
                            <div class="panel-body">
                                <div class="col-md-12">
                                    <label for="project_document_no">Document No. (เลขที่)</label>
                                    <input type="text" id="project_document_no" name="project_document_no" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_plan">Plan (แบบบ้าน)*</label>
                                    <input type="text" id="project_plan" name="project_plan" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_plot">Plot (แปลงบ้าน)*</label>
                                    <input type="text" id="project_plot" name="project_plot" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_po_owner">PO Owner. (เจ้าของ PO)</label>
                                    <input type="text" id="project_po_owner" name="project_po_owner" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_po_sender">PO Sender. (จนท. PO)</label>
                                    <input type="text" id="project_po_sender" name="project_po_sender" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_issue_date">Issue Date (วันที่)</label>
                                    <input type="text" id="project_issue_date" name="project_issue_date" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_order_type">Order Type (ประเภทงาน)</label>
                                    <select class="form-control" id="project_order_type" name="project_order_type" disabled="true">
                                        <option value="0">---Please select---</option>
                                        <?php
                                        $sqlSelectProjectType = "SELECT * FROM QRC_TYPE_OF_SERVICE;";
                                        $resultSet = mysql_query($sqlSelectProjectType);
                                        while ($row = mysql_fetch_array($resultSet)) {
                                            echo '<option value="' . $row['service_id'] . '">' . $row['service_name'] . '</option>';
                                        }
                                        ?>      
                                    </select>
                                    <div class="separator"></div>
                                </div>
                                <div class="col-md-12">
                                    <label for="project_plan_size">Plan Size (ขนาด ตร.ม.)</label>
                                    <input type="text" id="project_plan_size" name="project_plan_size" class="col-md-6 form-control" disabled="true"/>
                                    <div class="separator"></div>
                                </div>

                            </div>
                        </div>
                    </div>

                </div>

                <div class="col-md-12" style="visibility: hidden;">
                    <label for="project_unit_price">Unit Price (ราคาต่อหน่วย)</label>
                    <input type="text" id="project_unit_price" name="project_unit_price" class="col-md-6 form-control" disabled="true"/>
                    <div class="separator"></div>
                </div>
                <div class="col-md-12" style="visibility: hidden;">
                    <label for="project_amount">Amount (ราคารวม)</label>
                    <input type="text" id="project_amount" name="project_amount" class="col-md-6 form-control" disabled="true"/>
                    <div class="separator"></div>
                </div>
                <div class="col-md-12" style="visibility: hidden;">
                    <label for="project_vat">Grand Total included VAT 7%</label>
                    <input type="text" id="project_vat" name="project_vat" class="col-md-6 form-control" disabled="true"/>
                    <div class="separator"></div>
                </div>


            </div>
        </div>
        <!-- /.row -->
    </div>
    <input type="hidden" id="project_po_no_name">
    <input type="hidden" id="project_oid">
    <input type='hidden' id='service_name_value'/>
    <!-- /#page-wrapper -->

    <!-- jQuery Version 1.11.0 -->
    <script src="../js/jquery-1.11.0.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="../js/bootstrap.min.js"></script>

    <!-- Metis Menu Plugin JavaScript -->
    <script src="../js/plugins/metisMenu/metisMenu.min.js"></script>
    <script src="../js/jquery.toastmessage.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="../js/sb-admin-2.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/jquery-ui-1.10.4.custom.css" />
    <script type="text/javascript" src="../js/jquery-ui-1.10.4.custom.js"></script>
    <script type="text/javascript" src="../js/jquery.multiselect.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/jquery.multiselect.css" />
    <script>
        $(document).ready(function() {
            updateUpperMenu();
//            $('html,body').animate({scrollTop: $('#team_tbl_content').offset().top}, 400);
            $("#alert_inform").hide();
            $("#left-menu-load").load("../leftmenu-page/index-left-menu.php");
            var m_names = new Array("มกราคม", "กุมภาพันธ์", "มีนาคม",
                    "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน",
                    "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
            var d = new Date();
            var curr_date = d.getDate();
            var curr_month = d.getMonth();
            var curr_year = d.getFullYear();
            $("#project_issue_date").val(curr_date + " " + m_names[curr_month]
                    + " " + (curr_year + 543));
            $("#wo_assign_date").val(curr_date + " " + m_names[curr_month]
                    + " " + (curr_year + 543));
            $("#assign_case, #complete_case").hide();
            $(".search_date").datepicker();
            $("#update_project_status").load("../menu-page/project_menu_page.php", function() {
                $(".spinner").hide();
            });
            $("#reset_wo_form").click(function() {
                window.location.assign("index-assign.php");
            });
            $("#wo_team_code").change(function() {
                var tCode = $("#wo_team_code").val();
                var jqxhr = $.post("../model/GetAllTeamForEdit.php?teamID=" + tCode);
                jqxhr.success(function(data) {
                    //$.unblockUI();
                    obj = JSON.parse(data);
                    $("#wo_team_name").val(obj.t_Name);
                });
                jqxhr.error(function(data) {
                    window.location.replace("error.php?error_msg=" + data);
                });
            });

            $("#wo_price").change(function() {
                if ($("#project_amount").val() == "") {
                    $("#alert_inform").show();
                    $("#alert_information").html('<br/>- Please select PO Code(เลขที่ใบสั่งจ้าง)');
                    $('html,body').animate({scrollTop: $('#alert_inform').offset().top}, 400);
                } else {
                    $("#alert_inform").hide();
                    var result = ($("#wo_price").val() * 100) / $("#project_amount").val();
                    $("#perc_of_po").val(result);
                }
            });
            $("#perc_of_po").change(function() {
                if ($("#project_amount").val() == "") {
                    $("#alert_inform").show();
                    $("#alert_information").html('<br/>- Please select PO Code(เลขที่ใบสั่งจ้าง)');
                    $('html,body').animate({scrollTop: $('#alert_inform').offset().top}, 400);
                } else {
                    $("#alert_inform").hide();
                    var result = ($("#perc_of_po").val() * $("#project_amount").val()) / 100;
//                    $("#perc_of_po").val(result);
                    $("#wo_price").val(result);
                }
            });

            $("#logout_click").click(function() {
                var jqxhr = $.post("../model/LogoutDesSession.php");
                jqxhr.success(function(data) {
                    alert(data);
                    window.location.assign("../index.php")
                });
                jqxhr.error(function() {
                    alert("ไม่สามารถติดต่อกับ Server ได้");
                });
            });
            $("#po_no_no").change(function() {
                var po_id = $("#po_no_no").val();
                var jqxhr = $.post("../model/GetWorkOrderByPOID.php?po_id=" + po_id);
                jqxhr.success(function(data) {
                    obj = JSON.parse(data);
                    $("#project_plan").val(obj.PO_HOME_PLAN);
                    $("#project_plot").val(obj.PO_HOME_PLOT);
                    $("#project_document_no").val(obj.PO_DOCUMENT_NO);
                    $("#project_po_owner").val(obj.PO_OWNER);
                    $("#project_po_sender").val(obj.PO_SENDER);
                    $("#project_order_type").val(obj.order_type_name);
                    $("#project_plan_size").val(obj.PO_PLAN_SIZE);
                    $("#project_unit_price").val(obj.PO_UNIT_PRICE);
                    $("#project_amount").val(obj.PO_AMOUNT);
                    $("#project_vat").val(obj.PO_VAT);
                    $("#project_po_no_name").val(obj.PO_PO_NO);
                    $("#project_po_no_id").val(obj.po_id);
                    //$.unblockUI();
                    var orderType = $("#wo_order_type").val();
                    if ($("#po_no_no").val() == 0 || $("#po_no_no").val() == "0") {
                        $("#wo_name").val("");
                    } else {
                        $("#wo_name").val(orderType + obj.service_name + " " + obj.PO_HOME_PLAN + obj.PO_HOME_PLOT);
                    }
                    $("#service_name_value").val(obj.service_name);
                });
                jqxhr.error(function(data) {
                    window.location.replace("error.php?error_msg=" + data);
                });
            });
            $("#wo_order_type").change(function() {
                var orderType = $("#wo_order_type").val();
                var po_value = $("#po_no_no").val();
                if (po_value == "null" || po_value == null) {

                } else {
                    if (po_value != 0 || po_value != "0") {
                        $("#wo_name").val($("#wo_order_type").val() + $("#service_name_value").val() + " " + $("#project_plan").val() + $("#project_plot").val());
                    }
                }

            });
            $("#inspection_order_type_form").change(function() {
                var insID = $(this).val();
                var jqxhr = $.post("../model/GetInspectionByID.php?ins_id=" + insID);
                jqxhr.success(function(data) {
                    obj = JSON.parse(data);
                    $("#inspection_date_form").val(obj.INS_DATE);
                });
                jqxhr.error(function(data) {
                    window.location.replace("error.php?error_msg=" + data);
                });
            });
            $("#project_code_orderPage").change(function() {
                var projectCodeVal = $("#project_code_orderPage").val();
                var jqxhr = $.post("../model/GetPoByProjectID.php?project_code=" + projectCodeVal);
                jqxhr.success(function(data) {
                    $("#po_no_no").html(data);
                });
                jqxhr.error(function(data) {
                    window.location.replace("error.php?error_msg=" + data);
                });
            });


            $("#project_order_status").change(function() {
                if ($(this).val() == "Assign") {
                    var project_code = $("#project_code_orderPage").val();
                    var jqxhr = $.post("../model/GetAllProjectForEdit.php?project_code=" + project_code);
                    jqxhr.success(function(data) {
                        //$.unblockUI();
                        obj = JSON.parse(data);
                        $("#wo_project_managers").val(obj.project_manager);
                        $("#wo_project_foreman").val(obj.project_foreman);
                        $("#wo_supervisor_control").val(obj.supervisor_control);
                        setTimeout(function()
                        {
                            $("#assign_case").show("fast");
                        }
                        , 300);
                    });
                    jqxhr.error(function(data) {
                        window.location.replace("error.php?error_msg=" + data);
                    });
                    $("#assign_case").show("fast");
                    $("#complete_case").hide("fast");
                } else if ($(this).val() == "Complete") {
                    $("#assign_case").hide("fast");
                    $("#complete_case").show("fast");
                } else {
                    $("#assign_case").hide("fast");
                    $("#complete_case").hide("fast");
                }
            });
        });
        $("#save_wo_form").click(function() {
            var project_code = $("#project_code_orderPage").val();
            var project_order_status = $("#project_order_status").val();
            var project_home_plan = $("#project_plan").val();
            var project_home_plot = $("#project_plot").val();
            var project_document_no = $("#project_document_no").val();
            var project_po_no = $("#project_po_no_name").val();
            var project_po_owner = $("#project_po_owner").val();
            var project_po_sender = $("#project_po_sender").val();
            var project_order_type = $("#project_order_type").val();
            var project_plan_size = $("#project_plan_size").val();
            var project_unit_price = $("#project_unit_price").val();
            var project_amount = $("#project_amount").val();
            var project_vat = $("#project_vat").val();
            var poid = $("#project_po_no_id").val();
            var poForEdit = $("#po_no_no").val();
            var project_order_remark = $("#project_order_remark").val();
            var order_id = $("#project_oid").val();
            var wo_name = $("#wo_name").val();
            var wo_team_code = $("#wo_team_code").val();
            var teamCode = $("#wo_team_code").val();
            var wo_assign_date = $("#wo_assign_date").val();
            var wo_target_date = $("#wo_target_date").val();
            var wo_remark = $("#wo_remark").val();
            var orderType = $("#wo_order_type").val();
//            alert(orderType);
            var wo_price = $("#wo_price").val();
            var perc_of_po = $("#perc_of_po").val();
            // alert(project_order_status);
            //Save State
            if (project_code == 0) {
                $("#alert_inform").show();
                $("#alert_information").html('<br/>- Please select project Code(หมายเลขโครงการ)');
                $('html,body').animate({scrollTop: $('#alert_inform').offset().top}, 400);
            }
            if ($("#po_no_no").val() == 0) {
                $("#alert_inform").show();
                $("#alert_information").html('<br/>- Please select PO Code(เลขที่ใบสั่งจ้าง)');
                $('html,body').animate({scrollTop: $('#alert_inform').offset().top}, 400);
            }
            if ($("#wo_price").val() == "" || $("#perc_of_po").val() == "" || $("#wo_price").val() == "0" || $("#perc_of_po").val() == "0") {
                $("#alert_inform").show();
                $("#alert_information").html('<br/>- Please enter WO Price or % Of PO');
                $('html,body').animate({scrollTop: $('#alert_inform').offset().top}, 400);
            }
            if (project_code != 0 && $("#po_no_no").val() != 0 && $("#wo_price").val() != "" && $("#perc_of_po").val() != "" && $("#wo_price").val() != "0" && $("#perc_of_po").val() != "0") {
                if (project_order_status == "New") {

                    var jqxhr = $.post("../model/SavingProjectOrder.php?project_code=" + project_code +
                            "&project_order_status=" + project_order_status +
                            "&project_home_plan=" + project_home_plan +
                            "&project_home_plot=" + project_home_plot +
                            "&project_document_no=" + project_document_no +
                            "&project_po_no=" + project_po_no +
                            "&project_po_owner=" + project_po_owner +
                            "&project_po_sender=" + project_po_sender +
                            "&project_order_type=" + project_order_type +
                            "&project_plan_size=" + project_plan_size +
                            "&project_unit_price=" + project_unit_price +
                            "&project_amount=" + project_amount +
                            "&project_image_path=" + poForEdit +
                            "&wo_name=" + wo_name +
                            "&orderType=" + orderType +
                            "&project_vat=" + project_vat +
                            "&wo_price=" + wo_price +
                            "&perc_of_po=" + perc_of_po);
                    jqxhr.success(function(data) {
                        if (data == 1) {
                            $().toastmessage('showSuccessToast', 'บันทึกข้อมูลเรียบร้อยแล้ว');
                            window.location.assign("index-assign.php");
                        } else {
                            $().toastmessage('showErrorToast', 'ไม่สามารถบันทึกข้อมูลได้');
                        }
                    });
                    jqxhr.error(function(data) {
                        window.location.replace("error.php?error_msg=" + data);
                    });
                }
            }


        });
        function updateUpperMenu() {
            $("#load_all_status").load("../menu-page/load_all_status_page.php", function() {
                setTimeout(function()
                {
                    //Update Project
                    $("#update_project_status").load("../menu-page/project_menu_page.php", function() {
                        $(".spinner").hide();
                    });
                    //Update TaskAssignment 
                    $("#update_assign_status").load("../menu-page/taskassign_menu_page.php", function() {
                        $(".spinner").hide();
                    });
                    //Update po inspection 
                    $("#update_poinspection_status").load("../menu-page/poinspection_menu_page.php", function() {
                        $(".spinner").hide();
                    });
                    //Update team member update_team_member_status
                    $("#update_team_member_status").load("../menu-page/teammember_menu_page.php", function() {
                        $(".spinner").hide();
                    });
                    $("#left-menu-load").load("../leftmenu-page/index-left-menu.php");
                }
                , 100);
            });

        }
    </script>
</body>

</html>
